/* MINIX: #include "dfs.h" */
#include "sglib.h"

/* MAC OS TESTING */
#include <limits.h>
#include <fcntl.h>

#define OK 1
#define NOT_FOUND 0
#define FOUND 1

#define CMD_SIZE 100
#define UNIFIED_DIFF_CMD "diff -u "
#define APPEND ">"
#define SPACE " "
#define REPLICA_EXT ".tmp"
#define REPLICAS_FOLDER "tmp/"
#define DIFF_FILE "mods.diff"
#define PATCH_CMD "patch"
#define DELETE_CMD "rm"

#define HASH_TAB_SIZE 10
#define CACHEKEY_COMPARATOR(el1, el2)    (strcmp(el1->key,el2->key))

/* Hash function definitions */
#define NHASH 29989
#define MULT 31

typedef struct CacheElement{
	struct CacheElement *next;
	struct CacheElement *previous;
	char key[NAME_MAX];
	char value[PATH_MAX];
	int hits;
	int miss_st;
	int mbit;	 /* Modified (Dirty) bit */
	int rep_des;	 /* File descriptor of replica */
}CacheElement;

CacheElement *CacheTable[HASH_TAB_SIZE];

typedef struct CacheStats{
	int cache_sz;
	int fcells;
	int rcells;
}CacheStats;

unsigned int CacheElement_hash_function(CacheElement *el) {
	/* FIXME: Think about it more */
	unsigned int h = 0,i;
	for(i=1;i<NAME_MAX;i++)
		for(; el->key[i]; el->key[i]++)
			h = MULT * h + el->key[i];
	return h % NHASH;
}

/*
_PROTOTYPE( void init_cache, (void)								);
_PROTOTYPE( void destroy_cache, (void)							);
_PROTOTYPE( void insert_to_cache, (const char *, CacheElement *));
_PROTOTYPE( void update_cache_stats, (CacheElement *)			);
_PROTOTYPE( CacheElement * belongs_to_cache, (const char *)		);
_PROTOTYPE( void get_cache_stats, (CacheStats *)				);
_PROTOTYPE( void create_replica, (const char *)					);
_PROTOTYPE( void delete_replica, (const char *)					);
_PROTOTYPE( char * construct_diff_cmd, (const char *)			);
_PROTOTYPE( char * construct_patch_cmd, (void)					);
_PROTOTYPE( char * construct_delete_cmd, (const char *)			);
_PROTOTYPE( void apply_patch, (const char *, const char *)		);
*/

SGLIB_DEFINE_DL_LIST_PROTOTYPES(CacheElement, CACHEKEY_COMPARATOR, previous, next)
SGLIB_DEFINE_DL_LIST_FUNCTIONS(CacheElement, CACHEKEY_COMPARATOR, previous, next)
SGLIB_DEFINE_HASHED_CONTAINER_PROTOTYPES(CacheElement, HASH_TAB_SIZE, CacheElement_hash_function)
SGLIB_DEFINE_HASHED_CONTAINER_FUNCTIONS(CacheElement, HASH_TAB_SIZE, CacheElement_hash_function)
